﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DatNuocViet.Models;
using System.Net.Mail;

namespace DatNuocViet.Controllers
{
    public class ToursController : Controller
    {
        //
        // GET: /Tours/

        public ActionResult Index(int id)
        {
            ViewBag.nTours = Models.ToursDAO.getAmountOfToursByType(id);
            ViewBag.id = id;

            TourType type = ToursDAO.getTourType(id);

            ViewBag.Title = (Session["Language"].ToString() == "en") ? type.TourTypeEn : type.TourTypeVn;

            return View();
        }

        public ActionResult IndexPager(int? pageSize, int? pageNumber, int id)
        {
            int iPageNumber = (pageNumber ?? 1);
            int iPageSize = (pageSize ?? 10);

            List<Tour> listTours = Models.ToursDAO.GetToursByType(iPageNumber, iPageSize, id);
            return View(listTours);
        }

        public ActionResult Details(int id)
        {
            Tour tour = Models.ToursDAO.GetDetailTour(id);
            TourType type = ToursDAO.getTourType((int)tour.TourType);
            ViewBag.toursTypeId = type.Id;

            ViewBag.ToursType = (Session["Language"].ToString() == "en") ? type.TourTypeEn : type.TourTypeVn;
            ViewBag.Title = (Session["Language"].ToString() == "en") ? tour.NameEn : tour.NameVn;

            /*tour lien quan*/
            List<SearchTourResult> relateTours = new List<SearchTourResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                List<SearchTourResult> temp = db.SearchTour(1, 7, string.Empty, -1, -1, type.Id, "[Tours].[CreateOn]", "desc", string.Empty, string.Empty, -1, -1).ToList();
                relateTours = db.SearchTour(1, 7, string.Empty, -1, -1, type.Id, "[Tours].[CreateOn]", "desc", string.Empty, string.Empty, -1, -1).ToList();
                foreach (var item in temp)
                {
                    if (item.TourId == tour.Id)
                        relateTours.Remove(item);
                }
            }
            ViewData["RelateTours"] = relateTours;
            /*end*/

            return View(tour);
        }

        public ActionResult SearchTour(string keyword, int? tourtype, int? max, int? min)
        {
            try
            {
                string lang = (Session["Langauge"] == "en") ? "en" : "vn";
                
                ViewBag.keyword = HttpUtility.UrlEncode(keyword);
                ViewBag.type = (tourtype ?? -1);
                ViewBag.min = (min ?? -1);
                ViewBag.max = (max ?? -1);

                ViewBag.nTours = Models.ToursDAO.CountSearchTours(keyword,(tourtype ?? -1),(min ?? -1),(max ?? -1), lang);
                ViewBag.Title = (Session["Language"].ToString() == "en") ? "Finding tours" : "Tìm kiếm tours";

            }
            catch { return RedirectToAction("index", "home"); }

            return View();
        }

        public ActionResult SearchTourPager(int? pageSize, int? pageNumber, string keyword, int type, int min, int max)
        {
            int iPageNumber = (pageNumber ?? 1);
            int iPageSize = (pageSize ?? 10);
            string lang = (Session["Langauge"] == null) ? "vn" : "en";
            List<Tour> listTours = Models.ToursDAO.SearchTours(HttpUtility.UrlDecode(keyword), iPageNumber, iPageSize, type, min, max, lang);
            return View(listTours);
        }

        [HttpPost]
        public JsonResult FindToursNames(string searchText, int maxResults)
        {
            string lang = (Session["Langauge"] == null) ? "vn" : "en";

            var result = Models.ToursDAO.FindToursNames(searchText, maxResults, lang);

            return Json(result);
        }

        [HttpPost]
        public bool Booking(string hoten, string diachi, string dienthoai, string email, string ghichu, string tourname)
        {
            try
            {
                string fullname = hoten;
                string subject = "[Datnuocviet.com.vn] Đặt tour " + tourname;
                string content = "họ tên: " + hoten + " </br>" +
                    "Địa chỉ: " + diachi + " </br>" +
                        "Điện thoại: " + dienthoai + " </br>" +
                            "email: " + email + " </br>" +
                            "ghi chú: " + ghichu + " </br>";

                string mailbody = "<p>Họ tên người gửi: " + fullname + "</p>";
                mailbody += "<p>Địa chỉ email: " + email + "</p>";
                mailbody += "<p>Nội dung: " + content + "</p>";

                    MailMessage mail = new MailMessage();
                    SmtpClient cl = new SmtpClient();
                    mail.From = new MailAddress("dnv.mail01@gmail.com");
                    mail.To.Add("pethasonbao@gmail.com");
                    mail.IsBodyHtml = true;
                    mail.Subject = subject;
                    mail.Body = mailbody;
                    cl.Host = "smtp.gmail.com";
                    cl.Port = 587;
                    cl.EnableSsl = true;
                    cl.UseDefaultCredentials = false;
                    cl.Credentials = new System.Net.NetworkCredential("dnv.mail01@gmail.com", "PhongCanh123");
                    cl.Send(mail);
                    string message = DatNuocViet.Ultility.Helper.Label("mailsuccess");

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
